package com.yuwubao.hz_International.handle

import com.yuwubao.hz_International.exception.MoneyException
import com.yuwubao.hz_International.exception.SysException
import com.yuwubao.hz_International.util.Const
import com.yuwubao.hz_International.util.RestApiResponse
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody

@ControllerAdvice
class ExceptionHandle {
    val log = LoggerFactory.getLogger(ExceptionHandle::class.java)
    @ExceptionHandler(value = [(Exception::class)])
    @ResponseBody
    fun handle(e: Exception): RestApiResponse<Any> {
        val result = RestApiResponse<Any>()
        log.error("error:  $e")
        if (e is SysException) {
            result.failedApiResponse(Const.FAILED, "系统异常")
            return result
        }
        if (e is MoneyException) {
            result.failedApiResponse("余额不足")
            return result
        }
        result.failedApiResponse(e.message)
        return result
    }
}